<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class MakeUserGiftStatistics extends Command
{
    protected $signature = 'makeUserGiftStatistics';

    protected $description = '初始化用户收礼物统计';

    protected $giftStreamTable = 'gift_stream';
    protected $userGiftStatisticsTable = 'user_gift_statistics';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $list = DB::table($this->giftStreamTable)
            ->selectRaw('user_id, gift_id')
            ->groupBy('user_id', 'gift_id')
            ->get();
        $list = objToArray($list);
        foreach($list as $val) {
            $userId = $val['user_id'];
            $giftId = $val['gift_id'];

            //最初送礼
            $info = DB::table($this->giftStreamTable)
                ->where('user_id', $userId)
                ->where('gift_id', $giftId)
                ->first();
            $info = objToArray($info);
            $first_time = empty($info['change_timestamp']) ? '2023-06-01 00:00:00' : date('Y-m-d H:i:s', $info['change_timestamp']);
            $first_user_id = empty($info['from_user_id']) ? 0 : $info['from_user_id'];

            //最后送礼
            $info = DB::table($this->giftStreamTable)
                ->where('user_id', $userId)
                ->where('gift_id', $giftId)
                ->orderBy('stream_id', 'desc')
                ->first();
            $info = objToArray($info);
            $last_time = empty($info['change_timestamp']) ? '2023-06-01 00:00:00' : date('Y-m-d H:i:s', $info['change_timestamp']);
            $last_user_id = empty($info['from_user_id']) ? 0 : $info['from_user_id'];

            //数量
            $num = DB::table($this->giftStreamTable)
                ->where('user_id', $userId)
                ->where('gift_id', $giftId)
                ->sum('gift_num');

            $data = [
                'user_id' => $userId,
                'gift_id' => $giftId,
                'gift_num' => $num,
                'first_time' => $first_time,
                'first_user_id' => $first_user_id,
                'last_time' =>$last_time,
                'last_user_id' => $last_user_id,
            ];

            $info = DB::table($this->userGiftStatisticsTable)
                ->where('user_id', $userId)
                ->where('gift_id', $giftId)
                ->first();
            $info = objToArray($info);
            if(empty($info)) {
                $cacheKey = 'updateUserGiftStatistics:'.$userId.':'.$giftId;
                if (!redis_lock($cacheKey, 2)) {
                    print_r($data);
                    continue;
                }
                DB::table($this->userGiftStatisticsTable)->insertGetId($data);
                redis_unlock($cacheKey);
            }else{
                DB::table($this->userGiftStatisticsTable)->where('id', $info['id'])->update($data);
            }
        }

        return true;   
    }

}